import { defineStore } from 'pinia'
import { DatabaseService } from '../services/database'

export const useDBServiceStore = defineStore('dbService', {
  actions: {
    // 配置管理
    async getAllConfigs() {
      try {
        return await DatabaseService.getAllConfigs()
      } catch (error) {
        console.error('Failed to get all configs:', error)
        throw error
      }
    },
    
    // 表操作
    async listTables(configId, database) {
      try {
        return await DatabaseService.listTables(configId, database)
      } catch (error) {
        console.error(`Failed to list tables for ${database}:`, error)
        throw error
      }
    },
    
    async getTableColumns(configId, database, table) {
      try {
        return await DatabaseService.getTableColumns(configId, database, table)
      } catch (error) {
        console.error(`Failed to get columns for ${database}.${table}:`, error)
        throw error
      }
    },

    async getTableIndexes(configId, database, table) {
      try {
        return await DatabaseService.getTableIndexes(configId, database, table)
      } catch (error) {
        console.error(`Failed to get indexes for ${database}.${table}:`, error)
        throw error
      }
    },

    // 数据库操作
    async executeQuery(configId, query, args) {
      try {
        return await DatabaseService.executeQuery(configId, query, args || [])
      } catch (error) {
        console.error('Failed to execute query:', error)
        throw error
      }
    },

    async executeNonQuery(configId, query, args) {
      try {
        return await DatabaseService.executeNonQuery(configId, query, args || [])
      } catch (error) {
        console.error('Failed to execute non-query:', error)
        throw error
      }
    },

    // 批量执行SQL语句
    async executeBatchSQL(configId, sqlStatements) {
      try {
        return await DatabaseService.executeBatchSQL(configId, sqlStatements)
      } catch (error) {
        console.error('Failed to execute batch SQL:', error)
        throw error
      }
    }
  }
}) 